package com.study.services;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.study.model.jdao.IAddresdetailDAO;
import com.study.model.jdao.ICustomerDAO;
import com.study.model.jdao.IDeptDAO;
import com.study.model.jdao.IDictionaryDAO;
import com.study.model.jdao.IGoodDAO;
import com.study.model.jdao.ILinkmanDAO;
import com.study.model.jdao.IOffersdetailDAO;
import com.study.model.jdao.IOpportunityDAO;
import com.study.model.jdao.IOrderdetailDAO;
import com.study.model.jdao.IOrderformDAO;
import com.study.model.jdao.IUserDAO;
import com.study.model.mdao.AddressdetailMapper;
import com.study.model.mdao.AreaMapper;
import com.study.model.mdao.GoodDAO;
import com.study.model.mdao.LinkmanMapper;
import com.study.model.mdao.OfferDetailsDAO;
import com.study.model.mdao.OffersDAO;
import com.study.model.mdao.OrderFormDetailMapper;
import com.study.model.mdao.OrderFormMapper;
import com.study.model.pojo.Addresdetail;
import com.study.model.pojo.Customer;
import com.study.model.pojo.Good;
import com.study.model.pojo.Linkman;
import com.study.model.pojo.Offersdetail;
import com.study.model.pojo.Opportunity;
import com.study.model.pojo.Orderdetail;
import com.study.model.pojo.Orderform;
import com.study.model.vo.Cart;
import com.study.model.vo.CartBox;
import com.study.util.MaxId;

@org.springframework.stereotype.Service
@Transactional(rollbackOn = Exception.class)
public class OffersDetailsService {

	@Autowired
	OfferDetailsDAO mapper;
	@Autowired
	ILinkmanDAO linkmanDAO;
	@Autowired
	ICustomerDAO customerDAO;
	@Autowired
	IUserDAO userDAO;
	@Autowired
	IDictionaryDAO dictionaryDAO;
	@Autowired
	IOffersdetailDAO dao;
	@Autowired
	GoodDAO goodDAO;
	@Autowired
	IGoodDAO iGoodDAO;
	@Autowired
	IDeptDAO deptDAO;
	@Autowired
	OffersDAO offers;
	@Autowired
	IOrderdetailDAO orderdetailDAO;
	@Autowired
	IOrderformDAO orderformDAO;
	@Autowired
	OrderFormMapper morderform;
	@Autowired
	OrderFormDetailMapper morderdetail;
	@Autowired
	IOpportunityDAO oppDAO;
	@Autowired
	OrderFormMapper orderFormMapper;
	@Autowired
	AreaMapper areaMapper;
	@Autowired
	IAddresdetailDAO addressDAO;
	@Autowired
	AddressdetailMapper addressMapper;
	@Autowired
	LinkmanMapper linkmanMapper;

	/**
	 * 根据条件查询报价
	 * 
	 * @param i
	 * @param j
	 * @param string
	 * @param string2
	 * @return
	 */
	public List<JSONObject> selectBySel(int pageNum, int pageSize, String input) {
		PageHelper.startPage(pageNum, pageSize);
		return mapper.selectByWhere("%" + input + "%", "%" + input + "%");
	}

	/**
	 * 根据id查询一条记录
	 * 
	 * @param parameter
	 * @return
	 */
	public JSONObject selectBycid(String parameter) {
		return mapper.selectById(parameter);
	}

	/**
	 * 根据价格id查询一条记录
	 * 
	 * @param parameter
	 * @return
	 */
	public List<JSONObject> selecAlltBycid(String parameter) {

		List<JSONObject> details = mapper.selectAllById(parameter);
		return details;
	}

	/**
	 * 根据价格id查询一条cart
	 * 
	 * @param parameter
	 * @return
	 */
	public CartBox selecAlltBCart(String parameter) {

		List<JSONObject> details = mapper.selectAllById(parameter);
		List<Cart> carts = new ArrayList<>();
		int price = 0;
		int num = 0;
		for (JSONObject jsonObject : details) {
			Cart cart = new Cart();
			com.study.model.vo.Good goods = new com.study.model.vo.Good();
			goods.setGooId(jsonObject.getInteger("goo_id"));
			goods.setGooGunit(jsonObject.getInteger("goo_gunit"));
			goods.setGooName(jsonObject.getString("goo_name"));
			goods.setGooPrice(jsonObject.getBigDecimal("goo_price"));

			cart.setGoods(goods);
			Integer number = jsonObject.getInteger("number");
			cart.setQuatity(number);
			Integer sum = jsonObject.getInteger("sum");
			cart.setSum(sum);
			carts.add(cart);

			price = +sum;
			num = +number;

		}

		CartBox cartBox = new CartBox();
		cartBox.setMap(carts);
		cartBox.setPrice(price);
		cartBox.setQuatitys(num);
		return cartBox;
	}

	/**
	 * 删除维修工单
	 * 
	 * @param goo_id
	 */
	public void delete(int cid) {
		// 只进行逻辑删除
		Offersdetail offer = dao.findByOfferdetailId(cid);
		offer.setIsShow(0);
	}

	/**
	 * 编辑
	 * 
	 * @param theme
	 * @param user
	 * @param type
	 * @param dates
	 * @param times
	 * @param custname
	 * @param degree
	 * @param progress
	 * @param aresult
	 * @param spendtime
	 * @param feedback
	 * @param linkman
	 * @param more
	 * @param id
	 * @return
	 */
	public boolean editorService(String id, String phone, String cat, String state, String desc, String more,
			Date comdate) {

		return true;
	}

	public boolean addWeixiu(String name, String type, String dept, String custid, Date date1, String user,
			String linId, String phone, String money, String cat, String repair, String address, String state,
			String desc) {

		return true;
	}

	/**
	 * 维修状态统计
	 * 
	 * @return
	 */
	public List<JSONObject> selectByResult() {
		return mapper.selectByResult();
	}

	/**
	 * 查询所有商品
	 * 
	 * @return
	 */
	public List<JSONObject> findGoods() {

		return goodDAO.selectAll();
	}

	/**
	 * 转成订单
	 * 
	 * @param id
	 * @return
	 */
	public int toOrder(String id) {
		int aa = 3;

		// 获取价格明细详情
		List<JSONObject> details = mapper.selectAllById(id);
		// 获取价格单
		JSONObject offer = offers.selectById(id);
		// System.out.println(offer);

		int linkman = offer.getInteger("lin_id");
		JSONObject findByLinId = linkmanMapper.selectBylinid(linkman);
		// System.out.println(findByLinId);
		try {
			int adId = findByLinId.getInteger("ad_id");
		} catch (Exception e1) {
			return 1;
		}

		Integer customer = null;
		Integer  opportunity = null;
		Integer endmoney = null;
		Integer adId = null;
		Integer fills = null;
		try {
			customer = offer.getInteger("cus_id");
			opportunity = offer.getInteger("opp_id");
			endmoney = offer.getInteger("off_offer");
			fills = offer.getInteger("user_id");
			adId = findByLinId.getInteger("ad_id");
		} catch (Exception e1) {
			System.out.println("添加订单有问题");
			e1.printStackTrace();
		}
		
		System.out.println("我是销售机会"+endmoney);
		
		try {
			Integer findMaxId = orderFormMapper.findMaxId();
			String orderid = MaxId.generateNewOrderID(findMaxId);
			
			// 添加订单

			
			int result = morderform.doInsert(orderid, offer.getString("off_name"), customer, 1, 3001, 3006, opportunity,
					linkman, adId, new Date(), null, 3012, fills, new Date());
			
					
			Integer odnums	= 0;
			double odsums = 0;
			for (JSONObject jsonObject : details) {

				Integer goid = jsonObject.getInteger("goo_id");
				Integer odnum = jsonObject.getInteger("number");
				odnums = odnums + odnum;
				BigDecimal bigDecimal = jsonObject.getBigDecimal("price");
				double d = jsonObject.getDouble("sum");
				odsums = d + odsums;

				// 添加订单详情
				int result1 = morderdetail.insertinto(Integer.parseInt(orderid), goid, odnum, bigDecimal, d);

				if (result == 1 && result1 == 1) {
					// 设置单价单的状态为订单
					int result2 = offers.updateIsOrder(id);
						aa = 3;
				} else {
						aa = 4;
				}

			}
			System.out.println("今天的："+orderid);
			int row = morderform.updateSumCount(odsums, odnums, Integer.parseInt(orderid));
			System.out.println(row+"sssssssss");
		} catch (Exception e) {
			e.printStackTrace();
			aa = 2;
		}

		
		return aa;

	}

	/**
	 * 查询省市县
	 * 
	 * @return
	 */
	public List<JSONObject> getArea() {
		return areaMapper.findArea();
	}

	/**
	 * 设置联系人地址
	 * 
	 * @param adress
	 * @param id
	 * @return
	 */
	public String setAd(String adress, String id) {
		try {

			System.out.println("我来添加联系人了");

			// 获取id
			Integer findMaxId = addressMapper.findMaxId();
			String ad_id = MaxId.generateNewOrderID(findMaxId);

			// 获取价格单
			JSONObject offer = offers.selectById(id);
			int linid = offer.getInteger("lin_id");

			// 获取联系人
			JSONObject linkman = linkmanMapper.selectBylinid(linid);

			Addresdetail addresdetail = new Addresdetail();
			addresdetail.setAdId(Integer.parseInt(ad_id));
			addresdetail.setAdAddress(adress);
			addresdetail.setAdPhone(linkman.getString("lin_phone"));
			addresdetail.setAdMan(linkman.getString("lin_name"));
			addressDAO.save(addresdetail);

			int result = linkmanMapper.updateadId(ad_id, linid);
			System.out.println(ad_id + "------" + linid);
			System.out.println(result + "结果");

			return "ok";
		} catch (Exception e) {
			e.printStackTrace();
			return "fails";
		}

	}

	/**
	 * 删除对应id的订单详情
	 * 
	 * @param id
	 */
	public void deleteOrderDetail(Integer id) {
		mapper.deletOrderdetails(id);
	}

}
